package yonghe
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.events.Event;

	public class MyBtn extends Sprite
	{
		private var func:Function;
		private var _certer:Boolean = false;
		public function set center(b:Boolean):void
		{
			_certer = b;
			var i:int = numChildren;
			while(i>0)
			{
				--i;
				var bmp:Bitmap = getChildAt(i) as Bitmap;
				if(bmp)
				{
					if(b)
					{
						bmp.x = -bmp.width/2;
						bmp.y = -bmp.height/2;
					}else{
						bmp.x = 0;
						bmp.y = 0;
					}
				}
			}
		}
		public function MyBtn(func:Function=null,path:Object=null,path2:Object=null)
		{
			this.func = func;
			if(path as String){
				SwfLoader.SwfLoad("yonghe/"+path,loaded);
			}else if(path as DisplayObject){
				addChild(path as DisplayObject);
			}
			if(path2 as String){
				SwfLoader.SwfLoad("yonghe/"+path2,loaded2);
			}else if(path2 as DisplayObject){
				addChildAt(path2 as DisplayObject,0);
			}
			mouseChildren = false;
			buttonMode = true;
			addEventListener(MouseEvent.CLICK,clicked);
		}
		private function clicked(e:MouseEvent):void
		{
			if(func!=null)
				func();
		}
		private function loaded(e:Event):void
		{
			var bmp:Bitmap = null;
			if(e && e.type==Event.COMPLETE)
			{
				bmp = e.target.content as Bitmap;
			}
			addBg(bmp);
		}
		private function addBg(bmp:Bitmap):void
		{
			if(bmp)
			{
				addChild(bmp);
				center = _certer;
			}
		}
		private function loaded2(e:Event):void
		{
			var bmp:Bitmap = null;
			if(e && e.type==Event.COMPLETE)
			{
				bmp = e.target.content as Bitmap;
			}
			addBg(bmp);
		}
	}
}
